package kotlin.flying.com.android_study.activtity;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.ColorMatrix;
import android.graphics.ColorMatrixColorFilter;
import android.graphics.Paint;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.text.InputType;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.GridLayout;
import android.widget.ImageView;

import java.net.HttpURLConnection;

import kotlin.flying.com.android_study.R;

/**
 * Created by ourinfuku on 2017/12/2.
 */

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    private  static final  String TAG = MainActivity.class.getSimpleName();

    Bitmap bitmap;
    ImageView iv_photo;
    GridLayout matrixLayout;
    //每个edittext的宽高
    int mEtWidth;
    int mEtHeight;
    //保存20个edittext
    EditText[] mEts = new EditText[20];

    //一维数组保存20个矩阵值
    float[] mColorMatrix = new float[20];

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
//        setContentView(new AnimateView(this));
        bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.iv_model1);
        iv_photo = (ImageView) findViewById(R.id.iv_photo);
        matrixLayout = (GridLayout) findViewById(R.id.matrix_layout);
        Button btn_change = (Button) findViewById(R.id.btn_change);
        Button btn_reset = (Button) findViewById(R.id.btn_reset);
        btn_change.setOnClickListener(this);
        btn_reset.setOnClickListener(this);
        iv_photo.setImageBitmap(bitmap);
        float degrees = 36;
        double radians = degrees * Math.PI / 180d;

        Log.i(TAG ,String.valueOf(Math.PI * 30.3d / 180d));

        matrixLayout.post(new Runnable() {
            @Override
            public void run() {
                mEtWidth = matrixLayout.getWidth() / 5;
                mEtHeight = matrixLayout.getHeight() / 4;
                addEts();
                initMatrix();
            }
        });
    }
    //动态添加edittext
    private void addEts() {
        for (int i = 0; i < 20; i++) {
            EditText et = new EditText(this);
            et.setInputType(InputType.TYPE_CLASS_NUMBER | InputType.TYPE_NUMBER_FLAG_DECIMAL);
            mEts[i] = et;
            matrixLayout.addView(et, mEtWidth, mEtHeight);
        }
    }

    //初始化颜色矩阵
    private void initMatrix() {
        for (int i = 0; i < 20; i++) {
            if (i % 6 == 0) {
                mEts[i].setText(String.valueOf(1));
            } else {
                mEts[i].setText(String.valueOf(0));
            }
        }
    }
    //获取矩阵值
    private void getMatrix() {
        for (int i = 0; i < 20; i++) {
            String matrix = mEts[i].getText().toString();
            boolean isNone = null == matrix || "".equals(matrix);
            mColorMatrix[i] = isNone ? 0.0f : Float.valueOf(matrix);
            if (isNone) {
                mEts[i].setText("0");
            }
        }
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.btn_change:
                getMatrix();
                setImageMatrix();
                break;
            case R.id.btn_reset:
                //重置矩阵效果
                initMatrix();
                break;
        }

        //作用矩阵效果
        getMatrix();
        setImageMatrix();
    }
    //将矩阵设置到图像
    private void setImageMatrix() {
        Bitmap bmp = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(), Bitmap.Config.ARGB_8888);
        ColorMatrix colorMatrix = new ColorMatrix();
        colorMatrix.set(mColorMatrix);//将一维数组设置到ColorMatrix

        Canvas canvas = new Canvas(bmp);
        Paint paint = new Paint();
        paint.setColorFilter(new ColorMatrixColorFilter(colorMatrix));
        canvas.drawBitmap(bitmap, 0, 0, paint);
        iv_photo.setImageBitmap(bmp);
    }

    class AnimateView extends View{

        private float radius = 10;
        private Paint paint;

        public AnimateView(Context context) {
            super(context);
            Log.i(TAG , "执行的构造方法");
            paint = new Paint();
            paint.setColor(Color.YELLOW);
            paint.setStyle(Paint.Style.STROKE);
        }

        @Override
        public boolean onTouchEvent(MotionEvent event) {
            Log.i(TAG , "执行了事件的分发");
            return super.onTouchEvent(event);

        }

        @Override
        protected void onDraw(Canvas canvas) {
//            super.onDraw(canvas);
            Log.i(TAG , "onDraw画布的执行");
            canvas.translate(200 , 200);
            canvas.drawCircle(0 , 0 , radius++ , paint);

            if (radius > 100){
                radius = 10;
            }

            invalidate();
        }

        @Override
        protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
            super.onMeasure(widthMeasureSpec, heightMeasureSpec);
            Log.i(TAG , "onMeasure宽度："+ widthMeasureSpec + "高度：" + heightMeasureSpec);
        }

        @Override
        protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
            super.onLayout(changed, left, top, right, bottom);
            Log.i(TAG , "onLayout左边："+ left + "右边：" + right + "上边："+ top + "下边："+ bottom);
        }

        @Override
        protected void onSizeChanged(int w, int h, int oldw, int oldh) {
            super.onSizeChanged(w, h, oldw, oldh);
            Log.i(TAG , "onSizeChanged宽度" + w + h + oldw + oldh);
        }
    }
}
